package ldh.text;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import ldh.text.controller.MainController;
import ldh.text.util.ThreadPoolUtil;
import org.fxmisc.flowless.VirtualizedScrollPane;
import org.fxmisc.richtext.CaretNode;
import org.fxmisc.richtext.CodeArea;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;

public class VirtualizedScrollPaneDemo extends Application {

    private static final Logger LOGGER = LoggerFactory.getLogger(VirtualizedScrollPaneDemo.class);

    @Override
    public void start(Stage primaryStage) throws Exception {
        CodeArea codeArea = new CodeArea();
        VirtualizedScrollPane virtualizedScrollPane = new VirtualizedScrollPane(codeArea);
        virtualizedScrollPane.estimatedScrollYProperty().addListener(l->{
            ScrollBar scrollBar = (ScrollBar) virtualizedScrollPane.lookup("ScrollBar:vertical");
            LOGGER.info("scroll Y : {}, {}, {}", virtualizedScrollPane.getEstimatedScrollY(),
                    ((double)virtualizedScrollPane.totalHeightEstimateProperty().getOrElse(0.0)) - virtualizedScrollPane.getEstimatedScrollY(),
                    - scrollBar.getUnitIncrement() * 2  + virtualizedScrollPane.getEstimatedScrollY());

            virtualizedScrollPane.lookup("ScrollBar:vertical");
        });

        codeArea.setOnKeyPressed(e->{
            int length = codeArea.getText().length();
//            if (codeArea.getText(length-1, length))
//            codeArea.appendText("\r");
            codeArea.displaceCaret(codeArea.getText().length());
//            codeArea.addCaret(new CaretNode("key caret", codeArea, codeArea.getText().length()));
        });

        HBox hBox = new HBox();
        Button button = new Button("lookup");
        button.setOnAction(e->{
            Node node = virtualizedScrollPane.lookup("ScrollBar:vertical");
            LOGGER.info("lookup: {}", node);
        });
        hBox.getChildren().add(button);

        VBox vBox = new VBox(10);
        vBox.getChildren().addAll(virtualizedScrollPane, hBox);
        VBox.setVgrow(virtualizedScrollPane, Priority.ALWAYS);
        Scene scene = new Scene(vBox, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();


    }

    private void loadData(CodeArea codeArea) {
        ThreadPoolUtil.submit(()->{
            try {
                Path path = Paths.get("D:\\产业链金融\\测试\\gc\\catalina.out");
                RandomAccessFile randomAccessFile = new RandomAccessFile(path.toFile(), "r");
                FileChannel fileChannel = randomAccessFile.getChannel();
                ByteBuffer byteBuffer = ByteBuffer.allocate(10 * 1024 * 1024);
                fileChannel.read(byteBuffer);
                byteBuffer.flip();
                String content = new String(byteBuffer.array(), "utf8");
                Platform.runLater(()->{
                    codeArea.insertText(0, content);
                });
                fileChannel.close();
                randomAccessFile.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        });
    }
}
